package com.orangecy.common.result;

import org.apache.commons.collections.CollectionUtils;

import java.util.*;

/**
 * @Author : orangeCy.奕
 * @Product : 猎豹养成产品源码
 * @Time : 2021/11/5
 * @Desc : 分页返回封装,操作细节放在此类，逻辑收敛
 */
public class PagePlainResult<T> extends PlainResult {

    private int totalCount = 0;
    private int pageSize = 1;
    private int pageIndex = 1;
    /**
     * 分页子集合
     */
    private Collection<T> dataList;

    /**
     * 获取总条数
     * @return
     */
    public int getTotalCount(){
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 获取每页大小
     * @return
     */
    public int getPageSize(){
        return pageSize < 1 ? 1 : pageSize;
    }

    public int getPageIndex(){
        return pageIndex <1 ? 1 : pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        if(pageIndex <1) {
            this.pageIndex = 1;
        } else {
            this.pageIndex = pageIndex;
        }
    }

    public void setPageSize(int pageSize) {
        if(pageSize <1) {
            this.pageSize =1;
        }else{
            this.pageSize = pageSize;
        }
    }
    public List<T> getDataList(){
        return Objects.isNull(dataList) ? Collections.emptyList()
                : new ArrayList<>(dataList);
    }


    public int getTotalPages() {
        return this.totalCount % this.pageSize == 0 ? this.totalCount / this.pageSize
                : (this.totalCount / this.pageSize + 1);
    }

    public boolean isEmpty() {
        return CollectionUtils.isEmpty(dataList);
    }

    public static <T> PagePlainResult<T> create(int pageIndex,int pageSize) {
        PagePlainResult<T> resp = new PagePlainResult<>();
        resp.setSuccess(true);
        resp.setData(Collections.emptyList());
        resp.setTotalCount(0);
        resp.setPageSize(pageSize);
        resp.setPageIndex(pageIndex);
        return resp;
    }
    //todo 用到再更新

}